<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="result in results"
        :key="result.art_id"
        :title="result.title"
      />
    </van-list>
  </div>
</template>

<script>
import { getSearchResult } from '@/api/search'
export default {
  name: 'searchResult',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      page: 1, // 当前页码
      results: [],
      // 加载错误时 点击进行重新加载
      error: false
    }
  },
  methods: {
    async onLoad () {
      try {
        const { data } = await getSearchResult({
          page: this.page,
          per_page: 20,
          q: this.value
        })
        this.results.push(...data.data.results)
        if (data.data.results.length) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (err) {
        // 错误提示
        this.error = true
        console.log(err)
      }
      this.loading = false
    }
  }
}
</script>

<style>
</style>
